package dev.bong.service.impl;

import dev.bong.dto.LabRepairDTO;
import dev.bong.dto.RegisterDTO;
import dev.bong.mapper.LabMapper;
import dev.bong.mapper.LabRegisterMapper;
import dev.bong.mapper.LabRepairMapper;
import dev.bong.mapper.UserMapper;
import dev.bong.pojo.entity.LabRegister;
import dev.bong.pojo.entity.LabRepair;
import dev.bong.pojo.entity.User;
import dev.bong.service.TeacherService;
import dev.bong.utils.ThreadLocalUtil;
import dev.bong.vo.RegisterVO;
import dev.bong.vo.RepairVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/4/14 15:03
 * @version: 1.0
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private LabRepairMapper labRepairMapper;

    @Autowired
    private LabMapper labMapper;

    @Autowired
    private LabRegisterMapper labRegisterMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<RegisterVO> getRegisterList() {

        Map<String,Object> map=ThreadLocalUtil.get();
        Integer teacherId =(Integer) map.get("userId");
        String semester=(String)map.get("semester");
        String teacher= userMapper.getNameById(teacherId);
        List<LabRegister> list=labRegisterMapper.selectByTeacherIdAndSemester(teacherId,semester);

        List<RegisterVO> ans=new ArrayList<>();
        for(LabRegister tmp:list){

            RegisterVO vo= RegisterVO.builder()
                    .id(tmp.getId())
                    .semester(tmp.getSemester())
                    .registerStatus(tmp.getRegisterStatus())
                    .labType(tmp.getLabType())
                    .className(tmp.getClassName())
                    .studentClass(tmp.getStudentClass())
                    .studentNum(tmp.getStudentNum())
                    .weekStart(tmp.getWeekStart())
                    .weekEnd(tmp.getWeekEnd())
                    .section(tmp.getSection())
                    .teacherName(teacher)
                    .build();
            ans.add(vo);
        }
        return ans;
    }

    @Override
    public void addRegister(RegisterDTO dto) {
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer teacherId =(Integer) map.get("userId");
        String semester=(String)map.get("semester");




        LabRegister labRegister=LabRegister.builder()
                .semester(semester)
                .teacherId(teacherId)
                .registerStatus(-1)
                .labType(dto.getLabType())
                .className(dto.getClassName())
                .studentClass(dto.getStudentClass())
                .studentNum(dto.getStudentNum())
                .weekEnd(dto.getWeekEnd())
                .weekStart(dto.getWeekStart())
                .section(dto.getSection())
                .build();

        labRegisterMapper.addRegister(labRegister);

    }

    @Override
    public void updateRegister(RegisterDTO registerDTO, Integer registerId) {

        LabRegister labRegister=LabRegister.builder()
                .id(registerId)
                .labType(registerDTO.getLabType())
                .className(registerDTO.getClassName())
                .studentClass(registerDTO.getStudentClass())
                .studentNum(registerDTO.getStudentNum())
                .weekStart(registerDTO.getWeekStart())
                .weekEnd(registerDTO.getWeekEnd())
                .section(registerDTO.getSection())
                .build();
        labRegisterMapper.updateRegister(labRegister);

    }

    @Override
    public Integer getRegisterStatusById(Integer registerId) {

        return labRegisterMapper.getRegisterStatusById(registerId);
    }



    @Override
    public List<RepairVo> getRepairList() {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userId= (Integer) map.get("userId");

        String teacherName=userMapper.getNameById(userId);

        List<LabRepair> list=labRepairMapper.selectByTeacherId(userId);

        List<RepairVo> ans=new ArrayList<>();

        for(LabRepair tmp:list){
            RepairVo vo=RepairVo.builder()
                    .id(tmp.getId())
                    .labName(tmp.getLabNumber())
                    .labNumber(tmp.getLabNumber())
                    .repairStatus(tmp.getRepairStatus())
                    .repairSituation(tmp.getRepairSituation())
                    .repairTime(tmp.getRepairTime())
                    .faultDescription(tmp.getFaultDescription())
                    .teacherName(teacherName)
                    .build();
            ans.add(vo);
        }
        return ans;




    }


    @Override
    public Integer getLabIdByNumber(String labNumber) {
        return labMapper.getLabIdByLabNumber(labNumber);
    }

    @Override
    public void addRepair(LabRepairDTO dto) {

        Map<String,Object> map=ThreadLocalUtil.get();
        Integer teacherId= (Integer) map.get("userId");

        Integer labId=labMapper.getLabIdByLabNumber(dto.getLabNumber());
        LabRepair labRepair=LabRepair.builder()
                .labId(labId)
                .teacherId(teacherId)
                .labNumber(dto.getLabNumber())
                .faultDescription(dto.getFaultDescription())
                .repairTime(dto.getRepairTime())
                .repairStatus(-1)
                .build();

        labRepairMapper.addRepair(labRepair);
    }


}
